﻿/*
    Autor : Roger Medeiros
    Data  : 01/02/2012
    Site  : www.interacaosistemas.com.br
*/
using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.IO;

namespace i9PgAdmin.Core
{
    public class Agenda
    {
        public bool Segunda { get; set; }
        public bool Terca { get; set; }
        public bool Quarta { get; set; }
        public bool Quinta { get; set; }
        public bool Sexta { get; set; }
        public bool Sabado { get; set; }
        public bool Domingo { get; set; }
        public string Horas { get; set; }
        public string Minutos { get; set; }
        public string Caminho { get; set; }
        public bool Zipar { get; set; }

        private string Arquivo = "i9PgAdmin.Agenda.config";

        public Agenda()
        {
            this.Segunda = false;
            this.Terca = false;
            this.Quarta = false;
            this.Quinta = false;
            this.Sexta = false;
            this.Sabado = false;
            this.Domingo = false;
            this.Horas = "00";
            this.Minutos = "00";
            this.Caminho = "";
            this.Zipar = false;
        }

        public void Carrega()
        {
            if (!File.Exists(this.Arquivo))
            {
                this.Salva();
            }

            XmlTextReader config = new XmlTextReader(this.Arquivo);
            try
            {
                while (config.Read())
                {
                    if (config.Name == "Segunda")
                        this.Segunda = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Terca")
                        this.Terca = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Quarta")
                        this.Quarta = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Quinta")
                        this.Quinta = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Sexta")
                        this.Sexta = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Sabado")
                        this.Sabado = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Domingo")
                        this.Domingo = (config.ReadElementString().Trim() == "S");
                    if (config.Name == "Horas")
                        this.Horas = config.ReadElementString().Trim();
                    if (config.Name == "Minutos")
                        this.Minutos = config.ReadElementString().Trim();
                    if (config.Name == "Caminho")
                        this.Caminho = config.ReadElementString().Trim();
                    if (config.Name == "Zipar")
                        this.Zipar = (config.ReadElementString().Trim() == "S");
                }
                config.Close();
            }
            catch
            {
                config.Close();
                this.Salva();
                this.Carrega();
            }
        }

        public void Salva()
        {
            if (File.Exists(this.Arquivo))
            {
                File.Delete(this.Arquivo);
            }

            XmlTextWriter config = new XmlTextWriter(this.Arquivo, null);
            config.WriteStartDocument();

            config.WriteStartElement("configuracao");

            config.WriteElementString("Segunda", this.Segunda ? "S" : "N");
            config.WriteElementString("Terca", this.Terca ? "S" : "N");
            config.WriteElementString("Quarta", this.Quarta ? "S" : "N");
            config.WriteElementString("Quinta", this.Quinta ? "S" : "N");
            config.WriteElementString("Sexta", this.Sexta ? "S" : "N");
            config.WriteElementString("Sabado", this.Sabado ? "S" : "N");
            config.WriteElementString("Domingo", this.Domingo ? "S" : "N");
            config.WriteElementString("Horas", this.Horas.ToString());
            config.WriteElementString("Minutos", this.Minutos.ToString());
            config.WriteElementString("Caminho", this.Caminho.ToString());
            config.WriteElementString("Zipar", this.Zipar ? "S" : "N");

            config.WriteEndElement();
            config.WriteEndDocument();

            config.Flush();
            config.Close();
        }
    }
}
